require('chromedriver');
const webDriver = require('selenium-webdriver'),
    By = webDriver.By;
const driver = new webDriver.Builder()
    .forBrowser('chrome')
    .build();
const { login } = require('./login');
const { getSingle } = require('./getSingle');
const ExcelJs = require('exceljs');

async function play() {
    const workbook = new ExcelJs.Workbook()
    await workbook.xlsx.readFile('file/in/WB CP重量差异大截图.xlsx');
    const worksheet1 = workbook.getWorksheet(1);
    let trackingNumber = '';
    let index = 2;
    await driver.manage().window().setRect({width: 750, height: 600, x: 0, y: 0});
    await login('https://www.colisprive.com/agence/PageAgence/Colis/RechercherColis.aspx', driver, By);
    while (index > 1) {
        trackingNumber = worksheet1.getRow(index).getCell(2).value;
        if (!trackingNumber || trackingNumber.length === 0) {
            index = -1;
            return;
        }
        const numColis = trackingNumber.slice(0, 12);
        await getSingle(numColis, driver);
        // const imageId = workbook.addImage({
        //     base64: `data:image/png;base64,${base64}`,
        //     extension: 'png'
        // });
        // const imageId = workbook.addImage({
        //     filename: `file/out/D00000398184.png`,
        //     extension: 'png'
        // });
        // worksheet2.addImage(imageId, 'B2:D6');
        // console.log(`${trackingNumber} done`);
        // index = -1;
        console.log('完成第 %s', index);
        index++;
    }
    await driver.quit();
}

play().then(() => {
    console.log('截图完成');
});
